package com.tinycloud.miracll.product.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.tinycloud.common.core.text.UUID;
import com.tinycloud.common.core.utils.DateUtils;
import com.tinycloud.common.core.utils.SecurityUtils;
import com.tinycloud.common.datasource.annotation.Master;
import com.tinycloud.miracll.product.domain.NoModelExcelData;
import com.tinycloud.miracll.product.domain.ProductLine;
import com.tinycloud.miracll.product.domain.ProductPoint;
import com.tinycloud.miracll.product.domain.json.ScadaTagLogValues;
import com.tinycloud.miracll.product.mapper.ProductLineMapper;
import com.tinycloud.miracll.product.service.IProductPointService;
import com.tinycloud.miracll.product.util.ScadaUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tinycloud.miracll.product.service.IProductLineService;


/**
 * 产线管理Service业务层处理
 * 
 * @author tiny
 * @date 2021-08-20
 */
@Service
@Master
public class ProductLineServiceImpl implements IProductLineService 
{
    @Autowired
    private ProductLineMapper productLineMapper;

    @Autowired
    private IProductPointService productPointService;

    /**
     * 查询产线管理
     * 
     * @param id 产线管理主键
     * @return 产线管理
     */
    @Override
    public ProductLine selectProductLineById(String id)
    {
        return productLineMapper.selectProductLineById(id);
    }

    /**
     * 查询产线管理列表
     * 
     * @param productLine 产线管理
     * @return 产线管理
     */
    @Override
    public List<ProductLine> selectProductLineList(ProductLine productLine)
    {
        return productLineMapper.selectProductLineList(productLine);
    }

    /**
     * 新增产线管理
     * 
     * @param productLine 产线管理
     * @return 结果
     */
    @Override
    public int insertProductLine(ProductLine productLine)
    {
        productLine.setCreateTime(DateUtils.getNowDate());
        productLine.setId(UUID.randomUUID().toString());
        productLine.setCreateTime(DateUtils.getNowDate());
        productLine.setCreateBy(SecurityUtils.getUsername());
        return productLineMapper.insertProductLine(productLine);
    }

    /**
     * 修改产线管理
     * 
     * @param productLine 产线管理
     * @return 结果
     */
    @Override
    public int updateProductLine(ProductLine productLine)
    {
        productLine.setUpdateTime(DateUtils.getNowDate());
        productLine.setUpdateBy(SecurityUtils.getUsername());
        return productLineMapper.updateProductLine(productLine);
    }

    /**
     * 批量删除产线管理
     * 
     * @param ids 需要删除的产线管理主键
     * @return 结果
     */
    @Override
    public int deleteProductLineByIds(String[] ids)
    {
        return productLineMapper.deleteProductLineByIds(ids);
    }

    /**
     * 删除产线管理信息
     * 
     * @param id 产线管理主键
     * @return 结果
     */
    @Override
    public int deleteProductLineById(String id)
    {
        return productLineMapper.deleteProductLineById(id);
    }

    @Override
    public  List<NoModelExcelData> exportLineScadaData(List<String> ids, Date beginTime,Date endTime, Integer timeSpan, Integer count) {

        if(0==ids.size()) {
            List<ProductLine> productLineList = this.selectProductLineList(null);
            for (ProductLine productLine : productLineList ) {
                ids.add(productLine.getId());
            }
        }
        if (null==beginTime) {
            beginTime=  DateUtils.addDays(DateUtils.getNowDate(),-2) ;
        }
        if (null==count) {
            count =  17280;

        }
        if (null!=beginTime&&null!=endTime) {
            long betweenSec = DateUtil.between(beginTime,endTime, DateUnit.SECOND,true);
            double resultTem = betweenSec/5;
            count = new Double(Math.ceil(resultTem)).intValue();

        }
        List<NoModelExcelData> noModelExcelDataList = new ArrayList<NoModelExcelData>();
        String beginTimeString = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,beginTime);
        for (String id: ids) {
            NoModelExcelData noModelExcelData = new NoModelExcelData();
            String sheetName = null;
            List<String> head = new ArrayList<String>();
            List<List<String>> headList = new ArrayList<List<String>>();
            List<List<String>> excelDataList = new ArrayList<List<String>>();

            ProductLine productLineTem = this.selectProductLineById(id);
            if(null==productLineTem) {
                continue;
            }
            ProductPoint productPointTem  = new ProductPoint();
            productPointTem.setProductLineId(id);
            List<ProductPoint>  productPointList =  productPointService.selectProductPointList(productPointTem);
            if (null==productPointList ||0==productPointList.size()) {
                continue;
            }

            List<String> pointCode = new ArrayList<String>();
            sheetName = productLineTem.getProductCode();
            head.add("时间");
            headList.add(head);
            for (ProductPoint productPoint: productPointList) {
                head = new ArrayList<String>();
                pointCode.add(productPoint.getPointCode());
                head.add(productPoint.getPointCode());
                headList.add(head);
            }

            List<ScadaTagLogValues>  scadaTagLogValuesList = new ArrayList<ScadaTagLogValues>();
            for (String pointCodeTem:pointCode) {
                List<ScadaTagLogValues>  scadaTagLogValuesListTem = new ArrayList<ScadaTagLogValues>();
                List<String> pointCodeListTem = new ArrayList<String>();
                pointCodeListTem.add(pointCodeTem);
                scadaTagLogValuesListTem = ScadaUtil.getScadaTagValueLog(pointCodeListTem,beginTimeString,timeSpan,count);
                scadaTagLogValuesList.addAll(scadaTagLogValuesListTem) ;
            }

            for (int i=0;i<count;i++) {
                List   dataList = new ArrayList ();

                String timeString = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,DateUtils.addSeconds(beginTime,i*5));
                dataList.add(timeString);
                for (ScadaTagLogValues scadaTagLogValue : scadaTagLogValuesList ) {
                    List<String> valueList =  scadaTagLogValue.getValues();
                    dataList.add(valueList.get(i));

                }
                excelDataList.add(dataList);
            }

            noModelExcelData.setSheetName(sheetName);
            noModelExcelData.setHeadList(headList);
            noModelExcelData.setDataList(excelDataList);
            noModelExcelDataList.add(noModelExcelData);
            System.out.println("scadaTagLogValuesList  size:   "+scadaTagLogValuesList.size());
        }

        return  noModelExcelDataList;

    }
}
